package cn.stormbirds.stpaylib;

import android.app.Activity;
import android.os.AsyncTask;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.IOException;
import java.lang.ref.WeakReference;

import cn.stormbirds.stpaylib.bean.OrderRequest;
import cn.stormbirds.stpaylib.handler.BasePayHandler;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import static cn.stormbirds.stpaylib.STPaySDK.mOkHttpClient;
import static cn.stormbirds.stpaylib.STPaySDK.toPayURL;
import static cn.stormbirds.stpaylib.bean.STPayCodes.CODE_ERROR_CHARGE_REQUEST;

/**
 * Copyright (c) 小宝 @2019
 *
 * @ Package Name:    cn.stormbirds.stpaylib
 * @author         stormbirds
 * @ Email：          xbaojun@gmail.com
 * @ Created At：     2019/6/25 10:15
 * @ Description：
 */


public class PaymentTask extends AsyncTask<OrderRequest,Double,String> {
    private WeakReference<Activity>  mActivityReference;
    private PayType mPayType;
    private STPayCallback mCallback;

    public PaymentTask(Activity activity, PayType payType, STPayCallback callback) {
        this.mActivityReference = new WeakReference<>(activity);
        this.mPayType = payType;
        this.mCallback = callback;
    }

    @Override
    protected String doInBackground(OrderRequest... orderRequests) {
        String data = null;
        try {
            data = postJson(toPayURL, orderRequests[0]);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data;
    }

    /**
     * 获取到服务器订单，开始执行支付操作
     * @param preOrder
     */
    @Override
    protected void onPostExecute(String preOrder) {
        if( (preOrder ==null || preOrder.isEmpty()) && this.mCallback!=null){
            PayLog.d("获取预订单失败.");

            this.mCallback.onPayFinished(PayResult.makeResult(CODE_ERROR_CHARGE_REQUEST, mPayType.getChannelCode()));
        }else if(this.mCallback == null){
            PayLog.d("获取预订单失败.");
        }else{
            JSONObject res = JSON.parseObject(preOrder);
            JSONObject orderInfo = res.getJSONObject("orderInfo");
            if(orderInfo==null){
                this.mCallback.onPayFinished(PayResult.makeResult(CODE_ERROR_CHARGE_REQUEST, mPayType.getChannelCode()));
                return;
            }
            STPaySDK.isTest = res.getBoolean("isTest");
            BasePayHandler basePayHandler = this.mPayType.getPayService(this.mCallback,orderInfo);
            STPaySDK.mBasePayHandler = basePayHandler;
            basePayHandler.pay(this.mActivityReference.get());

        }
    }

    @Override
    protected void onPreExecute() {

    }

    /**
     * 从服务器获取预订单
     * @param url 服务器获取订单地址
     * @param order 订单详情
     * @return 预订单
     * @throws IOException
     */
    private String postJson(String url, OrderRequest order) throws IOException {
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        RequestBody requestBody = RequestBody.create(mediaType,JSONObject.toJSONString(order));
        /*new MultipartBody.Builder()
                .addFormDataPart("payId", String.valueOf(order.getPayId()))
                .addFormDataPart("price", String.valueOf(order.getPrice()))
                .addFormDataPart("transactionType", "APP")
                .build();*/
        Request request = new Request.Builder().post(requestBody).url(url).build();
        Response response = mOkHttpClient.newCall(request).execute();

//        assert response.body() != null;
        return response.code() == 200 ? response.body().string() : null;
    }

}
